

import java.io.IOException;

import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.rmi.PortableRemoteObject;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import sessionbeans.HelpRequestBeanRemote;

/**
 * Servlet implementation class ManageHelpServlet
 */
public class ManageHelpServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ManageHelpServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		InitialContext context;
		HelpRequestBeanRemote helpBean;
		String outcome = request.getParameter("outcome");
		String requestId = request.getParameter("requestid");
		
		if ((outcome != null) && (requestId != null)) {
			try {
				context = new InitialContext();
				Object ref = context.lookup("HelpRequestBeanRemote");
				helpBean = (HelpRequestBeanRemote)PortableRemoteObject.narrow(ref, HelpRequestBeanRemote.class);
				if (outcome.equals("accepted")) {
					helpBean.answerToHelpRequest(requestId,true);
				}
				else {
					helpBean.answerToHelpRequest(requestId,false);
				}
				response.sendRedirect("PendingHelpServlet");
				return;
			} catch (NamingException e) {
				System.out.println("Naming Exception");
				e.printStackTrace();
			}
		}
		System.out.println(requestId);
		response.sendRedirect("PendingHelpPage.jsp");
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
